package com.platform.car.service.impl;

import com.platform.car.dao.LockDao;
import com.platform.car.dao.ParkingDao;
import com.platform.car.dao.ParkingZoneDao;
import com.platform.car.dao.ParkinglotDao;
import com.platform.car.model.*;
import com.platform.car.service.ParkinglotService;
import com.platform.car.socket.SocketUtil;
import com.platform.common.base.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PakinglotServiceImpl extends BaseServiceImpl<Parkinglot> implements ParkinglotService {

    @Autowired
    private ParkinglotDao parkinglotDao;

    @Autowired
    private ParkingDao parkingDao;

    @Autowired
    private ParkingZoneDao parkingZoneDao;

    @Autowired
    private LockDao lockDao;

    @Override
    public Integer insert(Parkinglot parkinglot) throws Exception {
        return parkinglotDao.insert(parkinglot);
    }

    @Override
    public Integer deleteByIds(String[] ids) throws Exception {
        return parkinglotDao.deleteByIds(ids);
    }

    @Override
    public Parkinglot selectById(Integer id) {
        Parkinglot parkinglot = parkinglotDao.selectById(id);
        if (parkinglot != null){
            if(parkinglot.getParkingId() != null && !"".equals(parkinglot.getParkingId())){
                Parking parking = parkingDao.get(parkinglot.getParkingId());
                parkinglot.setParking(parking);
            }
            if(parkinglot.getZoneId() != null && !"".equals(parkinglot.getZoneId())){
                ParkingZone parkingZone = parkingZoneDao.get(parkinglot.getZoneId());
                parkinglot.setParkingZone(parkingZone);
            }
            if(parkinglot.getLockId() != null && !"".equals(parkinglot.getLockId())){
                Lock lock = lockDao.get(parkinglot.getLockId());
                parkinglot.setLock(lock);
            }
        }
        return parkinglot;
    }

    @Override
    public Integer updateById(Parkinglot parkinglot) throws Exception {
        return parkinglotDao.updateById(parkinglot);
    }

    @Override
    public List<Parkinglot> findByParkingId(Parkinglot parkinglot) {
        return parkinglotDao.findByParkingId(parkinglot);
    }

    @Override
    public List<Parkinglot> selectByParkingIdAndLock(Parkinglot parkinglot) {
        return parkinglotDao.selectByParkingIdAndLock(parkinglot);
    }

    @Override
    public void updateLotAndLock(Parkuse parkuse) {
        Parkinglot parkinglot = parkinglotDao.selectById(parkuse.getParkinglotId());
        if (parkinglot != null) {
            try {
                parkinglot.setSwitchStatus("1");//车位状态
                parkinglotDao.updateById(parkinglot);
                if (parkinglot.getLockId() != null && !"".equals(parkinglot.getLockId())) {
                    Lock lock = lockDao.get(parkinglot.getLockId());
                    if (lock != null) {
                        lock.setLockIo("0");
                        lock.setStatus("0");
                        lockDao.update(lock);
                        SocketUtil.socketTaskMap.get(lock.getNumber()).closeLock();
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }


}
